import requests
import json


def send_dingding_markdown(webhook_url, title, text, at_mobiles=None, is_at_all=False):
    """
    发送钉钉Markdown消息

    Args:
        webhook_url (str): 钉钉机器人的Webhook地址
        title (str): 消息标题
        text (str): Markdown格式的消息内容
        at_mobiles (list, optional): 需要@的手机号列表
        is_at_all (bool): 是否@所有人

    Returns:
        dict: 钉钉API返回的结果
    """
    # 构造请求数据
    data = {
        "msgtype": "markdown",
        "markdown": {
            "title": title,
            "text": text
        },
        "at": {
            "atMobiles": at_mobiles if at_mobiles else [],
            "isAtAll": is_at_all
        }
    }

    # 发送POST请求
    headers = {'Content-Type': 'application/json'}
    response = requests.post(webhook_url, data=json.dumps(data), headers=headers)

    # 返回结果
    return response.json()


# 使用示例
if __name__ == "__main__":
    # 钉钉机器人webhook地址
    webhook = "https://oapi.dingtalk.com/robot/send?access_token=b827a61478d0ed476e1a9529b22a2df6c468b16ef3a3cb14c29d5de7e515acea"

    # 消息标题和内容
    title = "测试消息"
    content = """
                ## 这是标题
                - 列表项1
                - **加粗文字**
                - [链接](http://www.example.com)
                    """

    # 发送消息
    result = send_dingding_markdown(webhook, title, content)
    print(result)
